package com.sunny.l59;

/**
 * @Author zsunny
 * @Date 2018/9/14 9:40
 * @Mail zsunny@yeah.net
 */
public class Solution {

    public static void main(String[] args) {

        Solution solution = new Solution();
        System.out.println(solution.numDecodings("301"));

    }

    public int numDecodings(String s) {
        if(null == s)
            return 0;
        int len = s.length();
        if(len == 0 || s.charAt(0) == '0')
            return 0;
        if(len == 1)
            return 1;

        int[] dp = new int[len];
        dp[0] =1;
        if((s.charAt(0) < '2' || (s.charAt(0) == '2' && s.charAt(1) <= '6')) && s.charAt(1) > '0'){
            dp[1] = 2;
        }else if(s.charAt(1) > '0'){
            dp[1] = 1;
        }else if(s.charAt(0) > '0' && s.charAt(0)<='2'){
            dp[1] = 1;
        }else{
            return 0;
        }
        for(int i=2;i<len;i++){
            if(((s.charAt(i-1) < '2' && s.charAt(i-1) > '0')
                    || (s.charAt(i-1) == '2' && s.charAt(i) <= '6'))
                    && s.charAt(i)>'0'){
                dp[i] = dp[i-2] + dp[i-1];
            }else if(s.charAt(i) > '0'){
                dp[i] = dp[i-1];
            }else if(s.charAt(i-1)>'0' && s.charAt(i-1)<='2'){
                dp[i] = dp[i-2];
            }else{
                return 0;
            }
        }
        return dp[len-1];
    }
}
